import { message } from 'antd';
import { routerRedux } from 'dva/router';
import { getUser, updateUser, register, login, logout, saveToken, getUserCookie } from '../services/users';

const ADMIN_MENU   = ['总体情况', '1组状态', '2组状态', '3组状态', '4组状态', '5组状态', '5组状态', '技术组', '退出登录'];
const MANAGER_MENU = ['目前状态', '历史消息', '个人资料', '本组情况', '退出登录'];
const USER_MENU    = ['目前状态', '留言箱', '个人资料', '退出登录'];

export default {
  namespace: 'users',
  state: {
    isLoggedIn: true,
    userid: "",
    username: "",
    phone: "",
    group: "",
    roll: 2,
    menu: USER_MENU,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'users/getUser'
          })
        } else if (pathname === '/home') {
          dispatch({
            type: 'users/getUserLocal'
          })
        }
      })
    }
  },

  effects: {
    *getUser({}, { call, put }) {
      const res = yield getUser();
      if (!res || res.err) {
        yield put(routerRedux.push('/login'));
      } else {
        const { userid, username, roll } = res.data.data;
        let menu;
        if (roll === 0) {
          menu = ADMIN_MENU;
        } else if (roll === 1) {
          menu = MANAGER_MENU;
        } else {
          menu = USER_MENU;
        }
        yield put({ type: 'save', payload: { userid, username, roll, menu }});
        yield put(routerRedux.push('/home'));
      }
    },

    *getUserInfo({}, { call, put }) {
      const res = yield getUser();
      if (!res || res.err) {
        yield put(routerRedux.push('/login'));
      } else {
        const { userid, username, phone, group, roll } = res.data.data;
        yield put({ type: 'save', payload: { userid, username, phone, group, roll }});
      }
    },

    *updateUserInfo({ payload }, { call, put }) {
      const userInfo = {};
      for (const key in payload) {
        if (payload.hasOwnProperty(key)
          && payload[key]
          && payload[key] != undefined
          && key != 'confirm'
          && key != 'prefix') {
          userInfo[key] = payload[key];
        }
      }
      const res = yield updateUser(userInfo);
      console.log(res);
      if (!res) {
        message.info('没有更新内容');
        return;
      }
      if (res.err) {
        message.error('更新失败');
        return;
      }
      const { status } = res.data;
      if (status === 200) {
        yield put({ type: 'save', payload: userInfo});
        message.success('更新成功');
      }
    },

    *getUserLocal({}, {call, put}) {
      const { userid, username, roll } = yield getUserCookie();
      let menu = [];
      if (roll == 0) {
        menu = ADMIN_MENU;
      } else if (roll == 1) {
        menu = MANAGER_MENU;
      } else {
        menu = USER_MENU;
      }
      if (userid && username) {
        yield put({ type: 'save', payload: { userid, username, roll, menu }});
      } else {
        yield put(routerRedux.push('/login'));
      }
    },

    *login({ username, password }, {call, put}) {
      const res = yield login(username, password);
      console.log(res);
      if (res.data && (res.data.status === 200)) {
        saveToken(res.data.data);
        yield put(routerRedux.push('/home'));
      } else {
        message.info('用户名或者密码不对');
      }
    },

    *logout({}, {call, put}) {
      yield logout();
      yield put(routerRedux.push('/login'));
    },

    *register({  userid, username, password, phone, group }, {call, put}) {
      const { data } = yield register(userid, username, password, phone, group);
      if (data.status === 201) {
        saveToken(data.data);
        yield put(routerRedux.push('/home'));
      }
    }
  },

  reducers: {
    'delete'(state, { payload: id }) {
      return state.filter(item => item.id !== id);
    },
    'save'(state, action) {
      return { ...state, ...action.payload };
    },
  },
};
